package com.linkage.educloud.ah.view.activity.classspace;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.dynamic.DynamicListAdapter;
import com.linkage.educloud.ah.bean.AccountData;
import com.linkage.educloud.ah.bean.DynamicBean;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.evenbus.DynamicEvent;
import com.linkage.educloud.ah.mvp.contract.dynamic.IDynamicListContract;
import com.linkage.educloud.ah.mvp.presenter.dynamic.DynamicListPresenter;
import com.linkage.educloud.ah.util.ImageLoader;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.linkage.educloud.ah.widget.PanelView;
import com.zzhou.common.library.util.LogUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.linkage.educloud.ah.evenbus.DynamicEvent.*;

/**
 * 班级空间首页
 * Created by Mr'Tang on 2018/9/17.
 */

public class DynamicListActivity extends BasicTitleActivity implements IDynamicListContract.IView, View.OnClickListener {

    @BindView(R.id.activity_cs_dynamic_bg)
    ImageView activity_cs_dynamic_bg;
    @BindView(R.id.activity_cs_suspension)
    LinearLayout activity_cs_suspension;
    @BindView(R.id.activity_cs_clazzlist)
    TextView activity_cs_clazzlist;
    @BindView(R.id.activity_cs_clazzalbum)
    TextView activity_cs_clazzalbum;
    @BindView(R.id.activity_cs_clazznotice)
    TextView activity_cs_clazznotice;
    @BindView(R.id.activity_cs_dynamiclist)
    XRecyclerView activity_cs_dynamiclist;
    @BindView(R.id.activity_cs_scroll)
    ScrollView activity_cs_scroll;
    @BindView(R.id.activity_cs_panel)
    PanelView activity_cs_panel;

    List<TextView> dynamic_type_list = new ArrayList<>();

    IDynamicListContract.IPresenter iPresenter;
    AccountData accountData;
    DynamicListAdapter adapter;
    String class_id = "999965779";
    String dynamic_id = "0";
    List<DynamicBean> mList = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_dynamiclist;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setStatusTranslate(false);
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        setTitle("班级空间");
        _mToolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.ic_dynamic_add));
        activity_cs_panel.installView(1, 1, null);
        iPresenter = new DynamicListPresenter(this, this);
        dynamic_type_list.add(activity_cs_clazzlist);
        dynamic_type_list.add(activity_cs_clazzalbum);
        dynamic_type_list.add(activity_cs_clazznotice);
        activity_cs_clazzlist.setOnClickListener(this);
        activity_cs_clazzalbum.setOnClickListener(this);
        activity_cs_clazznotice.setOnClickListener(this);
        changeTAB(activity_cs_clazzlist);
        initRecyclerView();
        accountData = AccountConfig.getLoginAccount();
        showZloading();
        iPresenter.getbackground(class_id);
        iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 0);


        activity_cs_dynamiclist.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                activity_cs_scroll.setVisibility(View.GONE);
                activity_cs_panel.clearED();
                return false;
            }
        });
    }

    //切换tab改变颜色
    private void changeTAB(TextView textView) {
        for (TextView textView1 : dynamic_type_list) {
            if (textView.getId() == textView1.getId()) {
                textView1.setTextColor(getResources().getColor(R.color.text_click));
            } else {
                textView1.setTextColor(Color.BLACK);
            }
        }
    }

    //初始化RecyclerView
    public void initRecyclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activity_cs_dynamiclist.setLayoutManager(layoutManager);
        activity_cs_dynamiclist.setItemAnimator(new DefaultItemAnimator());
        activity_cs_dynamiclist.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        activity_cs_dynamiclist.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);

        activity_cs_dynamiclist.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                showZloading();
                dynamic_id = "0";
                iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 0);
            }

            @Override
            public void onLoadMore() {
                showZloading();
                iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 0);
            }
        });
        adapter = new DynamicListAdapter(this, mList, class_id);
        adapter.setHasStableIds(true);
        activity_cs_dynamiclist.setAdapter(adapter);

        //请求点赞接口
        adapter.setOnpraiseClickListener(new DynamicListAdapter.OnpraiseClickListener() {
            @Override
            public void onItemClick(int position) {
                showZloading();
                if (mList.get(position).getIspriased() == -1) {
                    iPresenter.praisedynamicnew(position, mList.get(position), class_id, 0);
                } else {
                    iPresenter.praisedynamicnew(position, mList.get(position), class_id, -1);
                }
            }
        });

        //点击评论图标展示自定义输入框
        adapter.setOncommentClickListener(new DynamicListAdapter.OncommentClickListener() {
            @Override
            public void onItemClick(final int position) {
                activity_cs_scroll.setVisibility(View.VISIBLE);
                activity_cs_panel.andFocusable();
                activity_cs_dynamiclist.setClickable(false);

                //点击send按钮发送评论请求
                activity_cs_panel.setOnSendClickListener(new PanelView.OnSendClickListener() {
                    @Override
                    public void onSendClick(String text) {
                        showZloading();
                        activity_cs_dynamiclist.setClickable(true);
                        activity_cs_scroll.setVisibility(View.GONE);
                        activity_cs_panel.clearED();
                        iPresenter.senddynamicmsg(position, mList.get(position), class_id, accountData.getUserId(), accountData.getUserType(), text);
                    }
                });
            }
        });
    }

    //加载列表数据
    @Override
    public void initRecyclerViewDate(List<DynamicBean> mData) {
        hideZloading();
        for (DynamicBean list : mData) {
            LogUtils.e(list.getContent());
        }
        if (mData.size() < 10) {
            activity_cs_dynamiclist.setLoadingMoreEnabled(false);
        } else {
            activity_cs_dynamiclist.setLoadingMoreEnabled(true);
        }
        adapter.addAll(mData, !dynamic_id.equals("0"));
        dynamic_id = mData.get(mData.size() - 1).getDynamic_id();
    }

    //列表请求完的停止刷新
    public void stopRefresh() {
        activity_cs_dynamiclist.refreshComplete();
        activity_cs_dynamiclist.loadMoreComplete();
    }

    //点赞,评论接口请求完的ui刷新
    public void updatePraise(int pos, DynamicBean bean) {
        hideZloading();
        mList.set(pos, bean);
        adapter.notifyItemChanged(pos + 1);
    }

    //浏览类型状态切换
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_cs_clazzlist:
                showZloading();
                changeTAB(activity_cs_clazzlist);
                dynamic_id = "0";
                iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 0);
                break;
            case R.id.activity_cs_clazzalbum:
                showZloading();
                changeTAB(activity_cs_clazzalbum);
                dynamic_id = "0";
                iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 1);
                break;
            case R.id.activity_cs_clazznotice:
                showZloading();
                changeTAB(activity_cs_clazznotice);
                dynamic_id = "0";
                iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 2);
                break;
            default:
                break;
        }
    }

    public void setTopImage(String url) {
        ImageLoader.loadAds(this, url, activity_cs_dynamic_bg);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_write_dynamic, menu);
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelectedCompat(MenuItem item) {
        int i = item.getItemId();
        switch (i) {
            case R.id.menu_dynamic_send_text:
                DynamicWriterTextActivity.launch(this, 0, class_id);
                break;
            case R.id.menu_dynamic_send_pic:
                DynamicWriterTextActivity.launch(this, 1, class_id);
                break;
            case R.id.menu_dynamic_send_voice:
                DynamicWriterVoiceActivity.launch(this);
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public boolean onMenuOpened(int featureId, Menu menu) {
        if (menu != null) {
            if (Constants.MENU_BUILDER.equalsIgnoreCase(menu.getClass().getSimpleName())) {
                try {
                    @SuppressLint("PrivateApi")
                    Method method = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    method.setAccessible(true);
                    method.invoke(menu, true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(DynamicEvent dynamicEvent) {
        switch (dynamicEvent.getType()) {
            case 1:
                updatePraise(dynamicEvent.getPos(), dynamicEvent.getBean());
                break;
            case 2:
                adapter.removeData(dynamicEvent.getPos());
                break;
            case 3:
                updatePraise(dynamicEvent.getPos(), dynamicEvent.getBean());
                break;
            case 4:
                showZloading();
                changeTAB(activity_cs_clazzlist);
                dynamic_id = "0";
                iPresenter.getClassSpaceList(dynamic_id, 10, class_id, 0);
                break;
        }
    }
}
